<template>
  <Form class="xtx-form" :validation-schema="veeValidateSchema"
      v-slot="{ errors }" ref="formCom">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field :class="{err:errors.accountApi}"  name='accountApi' v-model="form.account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error">{{errors.accountApi}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field :class="{err:errors.mobile}"  name='mobile' v-model="form.mobile" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field :class="{err:errors.code}"  name='code' v-model="form.code" class="input" type="text" placeholder="请输入验证码" />
        <span class="code" @click="sendCode">{{time===0?'发送验证码':`${time}秒后发送`}}</span>
      </div>
      <div class="error">{{errors.code}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field :class="{err:errors.password}"  name='password' v-model="form.password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error">{{errors.password}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field :class="{err:errors.rePassword}"  name='rePassword' v-model="form.rePassword" class="input" type="password" placeholder="请确认密码" />
      </div>
      <div class="error">{{errors.rePassword}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="register">立即提交</a>
  </Form>
</template>

<script>
import { Form, Field } from 'vee-validate'
import veeValidateSchema from '@/utils/vee-validate-schema'
import { inject, reactive, ref } from 'vue'
import Message from '@/components/library/Message'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { useIntervalFn } from '@vueuse/shared'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  components: { Form, Field },
  setup () {
    const form = reactive({
      account: '',
      mobile: '',
      code: '',
      password: '',
      rePassword: ''
    })
    const qqUserInfo = inject('qqUserInfo')

    const formCom = ref(null)

    const time = ref(0)
    const { pause, resume } = useIntervalFn(() => {
      time.value--
      if (time.value <= 0) {
        pause()
      }
    }, 1000, { immediate: false })

    const sendCode = async () => {
      const validate = await veeValidateSchema.mobile(form.mobile)
      console.log(validate)
      if (validate === true) {
        try {
          if (time.value !== 0) return
          await userQQPatchCode(form.mobile)
          time.value = 60
          resume()
          Message({ type: 'success', message: '验证码发送成功' })
        } catch (error) {
          // console.dir(error.response.data.message)
          Message({ type: 'warn', message: error.response.data.message })
        }
      } else {
        formCom.value.setFieldError('mobile', validate)
      }
    }

    const store = useStore()
    const router = useRouter()

    const register = async () => {
      const validate = await formCom.value.validate()
      if (validate === true && qqUserInfo.value.openId) {
        try {
          console.log({
            unionId: qqUserInfo.value.openId,
            ...form
          })
          const { result } = await userQQPatchLogin({
            unionId: qqUserInfo.value.openId,
            ...form
          })
          console.log(result)
          store.commit('user/setUser', result)
          router.push(store.state.user.redirectUrl)
          Message({ type: 'warn', message: 'QQ完善信息成功' })
        } catch (error) {
          Message({ type: 'warn', message: 'QQ完善信息失败' })
        }
      } else {
        Message({ type: 'error', message: '表单校验失败' })
      }
    }

    return { veeValidateSchema, form, formCom, sendCode, time, register }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
